package com.niuxiaofa.newlaw.Activity;


import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.niuxiaofa.newlaw.Base.BaseActivity;
import com.niuxiaofa.newlaw.Bean.UserLoginBean;
import com.niuxiaofa.newlaw.Network.UserPresenter;
import com.niuxiaofa.newlaw.R;
import com.niuxiaofa.newlaw.Tools.Constant;
import com.niuxiaofa.newlaw.Tools.GetLog;
import com.niuxiaofa.newlaw.Tools.SystemUtil;
import com.niuxiaofa.newlaw.Tools.ToActivityUtil;
import com.niuxiaofa.newlaw.adapter.AutoPollAdapter;
import com.niuxiaofa.newlaw.adapter.AutoPollRecyclerView;
import com.wynsbin.vciv.VerificationCodeInputView;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import www.linwg.org.lib.LCardView;


public class RegisterActivity extends BaseActivity {

    @BindView(R.id.recyclerview)
    AutoPollRecyclerView recyclerView;

    boolean VcClick = true;

    @BindView(R.id.getcode)
    LCardView getcode;


    @BindView(R.id.phocode)
    VerificationCodeInputView phocode;


    @BindView(R.id.regetcode)
    TextView regetcode;

    private Handler mhandler = new Handler();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_register;
    }


    private int time = 60;

    String phone;

    SharedPreferences sharedPreferences;


    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            time--;
            VcClick = false;
            //将时间给他填进tv里面
            regetcode.setText("获取验证码(" + time + "s)");
            //当他的time==0的时候执行的跳转
            if (time == 0) {
                time = 60;
                VcClick = true;
                regetcode.setText("获取验证码");
            } else {
                mhandler.postDelayed(runnable, 1000);
            }
        }
    };


    @Override
    protected void initData() {

        setSystemInvadeBlack(false);
        sharedPreferences = getSharedPreferences("user", Context.MODE_PRIVATE);

        recyclerView.setLayoutManager(
                new LinearLayoutManager(
                        this,
                        //设置LinearLayoutManager.HORIZONTAL  则水平滚动
                        LinearLayoutManager.VERTICAL, false));
        AutoPollAdapter autoPollAdapter = new AutoPollAdapter(getApplicationContext());
        recyclerView.setAdapter(autoPollAdapter);
        //启动滚动
        recyclerView.start();

        getcode.setOnClickListener(e -> {

        });

        phone = getIntent().getStringExtra("phone");

        phocode.setOnInputListener(new VerificationCodeInputView.OnInputListener() {
            @Override
            public void onComplete(String code) {
                Map deviceMap = new HashMap();
                deviceMap.put("osType", "android");
                deviceMap.put("os", SystemUtil.getSystemVersion());
                 deviceMap.put("serialNumber", "");
                deviceMap.put("pushToken", "123456");

                Map map = new HashMap();
                map.put("userName", phone);
                map.put("smsCode", code);
                map.put("type", "SMS");
                map.put("accessToken", "");
                map.put("unionid", "");
                map.put("nickName", "");

                map.put("deviceLogin", deviceMap);


                UserPresenter.login(map, new UserPresenter.interface_login() {
                    @Override
                    public void getResponse(UserLoginBean u) {

                        if(u.getCode()==1){
                            Constant.TOKEN=u.getData().getToken();
                            Toast(u.getMsg());
                            GetLog.getLog( Constant.TOKEN);

                            SharedPreferences.Editor editor = sharedPreferences.edit();
                            editor.putString("token", Constant.TOKEN);
                            editor.commit();

                            ToActivityUtil.toNextActivity(MainActivity.class);

                        }
                    }
                });
            }

            @Override
            public void onInput() {

            }
        });


        mhandler.postDelayed(runnable, 1000);


    }


}
